/**
 * 作者:李杰
 * email: maillijie@gmail.com
 */
package com.javainfo.tags;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.javainfo.dto.PageDto;
import com.javainfo.util.Const;
import com.javainfo.util.StringUtils;

public class PageTag extends TagSupport {

	/** serialVersionUID */
	private static final long serialVersionUID = 1L;

	private String requestPath;
	private String requestParams;

	public int doStartTag() throws JspException {
		StringBuilder sb = new StringBuilder(128);
		PageDto pageDto = (PageDto) pageContext.getRequest().getAttribute(
				"pageDto");

		String appPath = pageContext.getServletContext().getServletContextName();
		String params = Const.EMPTY_STRING.equals(StringUtils
				.nvl(getRequestParams())) ? Const.EMPTY_STRING : (StringUtils
				.nvl(getRequestParams()) + "&");

		String actionPath = "<a href=\\" + appPath + StringUtils.nvl(getRequestPath())
				+ "?" + params + "pageDto.pageTo=";

		sb.append("<div style=\"text-align: right;\">");
		sb
				.append("<input type=\"hidden\" name=\"pageDto.currentPage\" value=\""
						+ pageDto.getCurrentPage() + "\">");
		sb.append("总共" + pageDto.getTotalPages() + "页  ");
		sb.append("当前第" + pageDto.getCurrentPage() + "页  ");
		if (pageDto.getCurrentPage() < 2) {
			sb.append("最前页");
		} else {
			sb.append(actionPath + 1 + ">最前页</a>");
		}
		if (pageDto.getCurrentPage() < 2) {
			sb.append("前一页");
		} else {
			sb.append(actionPath + (pageDto.getCurrentPage() - 1) + ">前一页</a>");
		}

		if (pageDto.getTotalPages() > 10) {
			if (pageDto.getCurrentPage() > 3
					&& pageDto.getTotalPages() - pageDto.getCurrentPage() > 7) {
				sb.append("...");
				for (int i = pageDto.getCurrentPage() - 3; i < (7 + pageDto
						.getCurrentPage()); i++) {
					if (pageDto.getCurrentPage() != (i + 1)) {
						sb.append(actionPath + (i + 1) + ">" + (i + 1)
								+ "</a>  ");
					} else {
						sb.append((i + 1) + "  ");
					}
				}
				sb.append("...");
			} else if (pageDto.getCurrentPage() > 3
					&& pageDto.getTotalPages() - pageDto.getCurrentPage() <= 7) {
				sb.append("...");
				for (int i = pageDto.getTotalPages() - 10; i < pageDto
						.getTotalPages(); i++) {
					if (pageDto.getCurrentPage() != (i + 1)) {
						sb.append(actionPath + (i + 1) + ">" + (i + 1)
								+ "</a>  ");
					} else {
						sb.append((i + 1) + "  ");
					}
				}
			} else {
				for (int i = 0; i < 10; i++) {
					if (pageDto.getCurrentPage() != (i + 1)) {
						sb.append(actionPath + (i + 1) + ">" + (i + 1)
								+ "</a>  ");
					} else {
						sb.append((i + 1) + "  ");
					}
				}
				sb.append("...");
			}
		} else {
			for (int i = 0; i < pageDto.getTotalPages(); i++) {
				if (pageDto.getCurrentPage() != (i + 1)) {
					sb.append(actionPath + (i + 1) + ">" + (i + 1) + "</a>  ");
				} else {
					sb.append((i + 1) + "  ");
				}
			}
		}

		if (pageDto.getCurrentPage() == pageDto.getTotalPages()) {
			sb.append("后一页");
		} else {
			sb.append(actionPath + (pageDto.getCurrentPage() + 1) + ">后一页</a>");
		}
		if (pageDto.getCurrentPage() == pageDto.getTotalPages()) {
			sb.append("最后页");
		} else {
			sb.append(actionPath + pageDto.getTotalPages() + ">最后页</a>");
		}
		sb.append("</div>");
		JspWriter out = pageContext.getOut();
		try {
			out.print(sb.toString());
		} catch (IOException e) {
		}
		return EVAL_BODY_INCLUDE;
	}

	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	public String getRequestPath() {
		return requestPath;
	}

	public void setRequestPath(String requestPath) {
		this.requestPath = requestPath;
	}

	public String getRequestParams() {
		return requestParams;
	}

	public void setRequestParams(String requestParams) {
		this.requestParams = requestParams;
	}

}
